package com.example.controller;

import java.util.ArrayList;

import com.example.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    
    @GetMapping(value = "/user",produces = "application/json;charset=utf-8")
    public String getUser() throws JsonProcessingException{
        User user = new User(15,"光头强","2020-09-09");
        JsonMapper jsonMapper = new JsonMapper();
        String jsonStr = jsonMapper.writeValueAsString(user);
        return jsonStr;
    }

    @GetMapping(value = "/users")
    public String getUsers() throws JsonProcessingException{

        User user1 = new User(15,"光头强","2020-09-09");
        User user2 = new User(16,"光头弱","2021-09-09");
        User user3 = new User(17,"光脚强","2022-09-09");
        User user4 = new User(18,"光脚弱","2023-09-09");

        ArrayList<User> users = new ArrayList<User>();

        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);

        return new JsonMapper().writeValueAsString(users);
    }

    @GetMapping("/user2")
    public String getUser2() throws JsonProcessingException{
        User user = new User(15,"光头强","2020-09-09");
        return new JsonMapper().writeValueAsString(user);
    }

}
